package com.itboye.hutouben.activity.luntan;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.adapter.TieZiMessageAdapter;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.base.ptr.BasePtr;
import com.itboye.hutouben.bean.SystemMessgeBean;
import com.itboye.hutouben.presenter.SysTemMessgelPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.UIAlertView02;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 帖子消息列表
 */
public class TieZiMessageActivity extends BaseActivity  implements Observer {
    TextView add_shap_title_tv,save;
    ImageView close_icon;
    PtrFrameLayout commend_anchor_ptr;
    private ListView messgeListview;
    private SysTemMessgelPresenter presenter;
    private List<SystemMessgeBean.ListEntity> list;

    private TieZiMessageAdapter adapter;
    int pageIndex = 1;

    LinearLayout kong_layoout;
    TextView kong_text;

    private UIAlertView02 uiAlertView;

    private Dialog dialog;// 自定义Dialog
    String deleteId;//删除的是哪一条
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_mess);
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);
        save.setVisibility(View.VISIBLE);
        list=new ArrayList<SystemMessgeBean.ListEntity>();
        add_shap_title_tv.setText("消息");
        save.setText("清空");
        showExtraDialog();
        presenter=new SysTemMessgelPresenter(this);
        showProgressDialog("加载中,请稍后", true);
        presenter.onSysTemMessge(IsUtilUid.isUid(),"6221",pageIndex+"","10");
        adapter=new TieZiMessageAdapter(MyApplcation.ctx,list);
        messgeListview.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageIndex++;
                presenter.onSysTemMessge(IsUtilUid.isUid(),"6221",pageIndex+"","10");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onSysTemMessge(IsUtilUid.isUid(),"6221",pageIndex+"","10");
            }
        });

        messgeListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                deleteId = list.get(position).getId();
                dialog.show();
                return true;
            }
        });

    }
    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.save://清空消息
                if(list.size()<=0){
                    ByAlert.alert("没有回复消息");
                    return;
                }
                uiAlertView = new UIAlertView02(TieZiMessageActivity.this, "温馨提示！", "是否删除全部消息？", "是", "取消");
                uiAlertView.show();
                uiAlertView.setClicklistener(new UIAlertView02.ClickListenerInterface() {
                    @Override
                    public void doLeft() {
                        showProgressDialog("刪除中,请稍后...",false);
                        presenter.deleteMess(IsUtilUid.isUid(),"6221","0");
                        uiAlertView.dismiss();
                    }
                    @Override
                    public void doRight() {
                        uiAlertView.dismiss();
                    }
                });
                break;

            default:
                break;
        }
    }
    @Override
    public void update(Observable observable, Object data) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity entity=handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType()==presenter.SystemMessge_success){
                if(pageIndex==1){
                    list.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                SystemMessgeBean bean= (SystemMessgeBean) entity.getData();
                list.addAll(bean.getList());

                if (list.size() > 0) {
                    adapter.notifyDataSetChanged();
                    kong_layoout.setVisibility(View.GONE);
                    messgeListview.setVisibility(View.VISIBLE);
                    commend_anchor_ptr.setVisibility(View.VISIBLE);

                } else {
                    kong_layoout.setVisibility(View.VISIBLE);
                    messgeListview.setVisibility(View.GONE);
                    commend_anchor_ptr.setVisibility(View.GONE);
                    kong_text.setText("消息为空");
                }

            }
            if (entity.getEventType()==presenter.SystemMessge_fail){
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType()==presenter.deletemMessge_success){
                ByAlert.alert(entity.getData());
                list.clear();
                adapter.notifyDataSetChanged();
                messgeListview.setVisibility(View.GONE);
                commend_anchor_ptr.setVisibility(View.GONE);
                kong_layoout.setVisibility(View.VISIBLE);
                kong_text.setText("消息为空");
            }
            if (entity.getEventType()==presenter.deletemMessge_fail){
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType()==presenter.deletemMessge_one_success){
                Iterator<SystemMessgeBean.ListEntity> iterator=list.iterator();
                while (iterator.hasNext()){
                    SystemMessgeBean.ListEntity bList=iterator.next();
                    if (deleteId.equals(bList.getId())){
                        iterator.remove();
                        adapter.notifyDataSetChanged();
                        if (list.size() > 0) {
                            adapter.notifyDataSetChanged();
                            kong_layoout.setVisibility(View.GONE);
                            messgeListview.setVisibility(View.VISIBLE);
                            commend_anchor_ptr.setVisibility(View.VISIBLE);

                        } else {
                            kong_layoout.setVisibility(View.VISIBLE);
                            messgeListview.setVisibility(View.GONE);
                            commend_anchor_ptr.setVisibility(View.GONE);
                            kong_text.setText("消息为空");
                        }
                        break;
                    }
                }
            }
            if (entity.getEventType()==presenter.deletemMessge_one_fail){
                ByAlert.alert(entity.getData());
            }
        }

    }


    /**
     * 删除Dialog
     */
    private void showExtraDialog() {
        dialog = new Dialog(this, R.style.rel_dialog);
        View view = LayoutInflater.from(this).inflate(
                R.layout.dialog_delete, null);
        dialog.setContentView(view);

        TextView dialog_ok = (TextView) view.findViewById(R.id.dialog_ok);
        TextView dialog_quxiao = (TextView) view.findViewById(R.id.dialog_quxiao);
        TextView user_title = (TextView) view.findViewById(R.id.user_title);
        TextView dialog_conext = (TextView) view.findViewById(R.id.dialog_conext);
        user_title.setText("删除消息");
        dialog_conext.setText("确定删除该消息？");

        dialog.setCanceledOnTouchOutside(false);
        dialog_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                showProgressDialog("刪除中,请稍后...",false);
                presenter.deleteMessOne(IsUtilUid.isUid(),"6221",deleteId);
            }
        });
        dialog_quxiao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        // 屏幕宽度
        int screenWidth = display.getWidth();
        // 屏幕高度
        int screenHeight = display.getHeight();
        Window window = dialog.getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        lp.width = screenWidth * 3 / 4;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

    }
}
